package com.twu.biblioteca;

public class ReservationState implements State {

    public State executeStateBehaviour(UserInterface userInterface, Database database) {

        if (LoginState.currentUser() == null) {
            userInterface.write(new Output("You Need to Log in First."));
            return new MenuState();
        }

        userInterface.write(new Output("Please Enter the Title of the Book or Movie You Would Like to Reserve:"));

        if (!verifyThatBookOrMovieExists(database.bookList(), database.movieList(), userInterface.read())) {
            userInterface.write(new Output("We Do Not Have That Book or Movie Yet."));
            return new MenuState();
        }

        userInterface.write(new Output("Reservation Successful."));

        return new MenuState();

    }

    private boolean verifyThatBookOrMovieExists(BookList bookList, MovieList movieList, Input input) {

        for (Book book : bookList) {

            if (input.string().equals(book.title())) {
                return true;
            }

        }

        for (Movie movie : movieList) {

            if (input.string().equals(movie.title())) {
                return true;
            }

        }

        return false;

    }

}
